local DamageFloatView = class("DamageFloatView",function() return display.newNode() end )

local NodePool = custom.NodePool:getInstance()

function DamageFloatView.create()
    return NodePool:getByKey("DamageFloatView") or DamageFloatView.new()
end

function DamageFloatView:ctor()
    local critTextNode = cc.CSLoader:createNode(Res.BattleEffect_CritText)
    local hurtTextNode = cc.CSLoader:createNode(Res.BattleEffect_HurtText)
    display.setCascadeOpacityEnabled(critTextNode,true)
    display.setCascadeOpacityEnabled(hurtTextNode,true)

    critTextNode:setVisible(false)
    hurtTextNode:setVisible(false)
    self:addChild(critTextNode)
    self:addChild(hurtTextNode)

    self._critTextNode = critTextNode
    self._hurtTextNode = hurtTextNode

    self._nodes = {
        {
            teamDamageLabel = critTextNode:getChildByName("team_h"):getChildByName("name"),
            foeDamageLabel = critTextNode:getChildByName("foe_h"):getChildByName("name"),
            teamHealLabel = critTextNode:getChildByName("team_c"):getChildByName("name"),
            foeHealLabel = critTextNode:getChildByName("foe_c"):getChildByName("name"),
        },
        {
            teamDamageLabel = hurtTextNode:getChildByName("team_h"):getChildByName("name"),
            foeDamageLabel = hurtTextNode:getChildByName("foe_h"):getChildByName("name"),
            teamHealLabel = hurtTextNode:getChildByName("team_c"):getChildByName("name"),
            foeHealLabel = hurtTextNode:getChildByName("foe_c"):getChildByName("name"),
        }
    }

    -- 新手战不需要跳字
    if not app:isBattleGuideComp() then
        self:setVisible( false )
    else
        self:setVisible( true )
    end
end

local StringBuilder = {"","","",""}
local CriticalText = L("criticaltext").." "
local BlockText = "("..L("blocktext")..")"
local LimitText = "("..L("limittext")..")"

function DamageFloatView:play(amount, isCC, isBlocked, isSelfTeam, isLimited)
    local node = nil
    local nodeIdx = 1
    if isCC then
        node = self._critTextNode
        nodeIdx = 1
    else
        node = self._hurtTextNode
        nodeIdx = 2
    end

    if not node then return end

    local subNodes = self._nodes[nodeIdx]

    local teamDamageLabel = subNodes.teamDamageLabel
    local foeDamageLabel = subNodes.foeDamageLabel
    local teamHealLabel = subNodes.teamHealLabel
    local foeHealLabel = subNodes.foeHealLabel

    if teamDamageLabel then
        teamDamageLabel:setVisible(false)
    end

    if foeDamageLabel then
        foeDamageLabel:setVisible(false)
    end

    if teamHealLabel then
        teamHealLabel:setVisible(false)
    end

    if foeHealLabel then
        foeHealLabel:setVisible(false)
    end

    local label = nil
    if amount < 0 then
        if isSelfTeam then
            label = teamHealLabel
        else
            label = foeHealLabel
        end
    else
        if isSelfTeam then
            label = teamDamageLabel
        else
            label = foeDamageLabel
        end
    end

    if label then
        label:setVisible(true)

        StringBuilder[1] = isCC and CriticalText or ""
        StringBuilder[2] = amount < 0 and "+" or "-"
        StringBuilder[3] = math.abs(math.floor(amount))
        StringBuilder[4] = isBlocked and BlockText or ""
        StringBuilder[5] = isLimited and LimitText or ""

        label:setString(table.concat(StringBuilder,""))
    end

    node:setVisible(true)
    local timeline = node:getTimeline()
    timeline:gotoFrameAndPlay(0,false)
    timeline:setLastFrameCallFunc(function()
        if not cc.isDead(node) then
            node:setVisible(false)
        end
        if not cc.isDead(self) then
            NodePool:put(self,"DamageFloatView")
            self:removeFromParent(false)
        end
    end)
    return timeline:getDuration()
end


return DamageFloatView
